package com.example.smart_campus.service.impl;

import com.example.smart_campus.common.Res;
import com.example.smart_campus.pojo.dto.grades.GradesTable;
import com.example.smart_campus.pojo.entity.Courses;
import com.example.smart_campus.pojo.entity.Grades;
import com.example.smart_campus.dao.GradesMapper;
import com.example.smart_campus.pojo.entity.Students;
import com.example.smart_campus.pojo.dto.grades.GradesItem;
import com.example.smart_campus.service.GradesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 成绩表，存储学生的成绩信息 服务实现类
 * </p>
 *
 * @author liyv
 * @since 2024-10-19
 */
@Service
public class GradesServiceImpl extends MPJBaseServiceImpl<GradesMapper, Grades> implements GradesService {

    @Autowired
    GradesMapper gradesMapper ;
    @Override
    public Res importByEList(List<GradesTable> tableList) {
        List<Grades> gradesList = new ArrayList<>();
        tableList.forEach(item->{
            Grades grade = new Grades(item);
            gradesList.add(grade);
        });
        int size = gradesList.size();

        if(gradesMapper.insertBatchSomeColumn(gradesList) == size) {
            return Res.ok() ;
        }
        return Res.failed() ;
    }

    @Override
    public Res<List<GradesItem>> getAllGradesByCid(String cid) {
        List<GradesItem> list = gradesMapper.selectJoinList(GradesItem.class,
                new MPJLambdaWrapper<Grades>()
                        .selectAll(Grades.class)
                        .selectAs(Students::getName,GradesItem::getCourseName)
                        .selectAs(Students::getStudentNo,GradesItem::getStudentNo)
                        .selectAs(Courses::getCourseName,GradesItem::getCourseName)
                        .leftJoin(Students.class, Students::getStudentId, Grades::getStudentId)
                        .leftJoin(Courses.class, Courses::getCourseId, Grades::getCourseId)
                        .eq(Courses::getCourseId,cid));
        return Res.ok(list);
    }
}
